<template>
  <div class="mobile-box">
    <div class="top">用户登录</div>
    <div class="form">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        class="login-form"
        @submit.native.prevent
      >
        <el-form-item label="" prop="userName">
          <el-input
            v-model="loginForm.userName"
            placeholder="手机号 / 账号"
            size="medium"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="登录密码"
            size="medium"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="form-bd">
          <el-button
            type="primary"
            size="medium"
            @click="submitLoginInfo('loginFormRef')"
            native-type="submit"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="undeline">
      <div>© dangan.qcdoc.cn</div>
      <div>访问完整 <a href="/retrieval/apply"> 电脑版</a></div>
    </div>
  </div>
</template>
<script>
import { userLogin, getAllChildList } from "@/api/member/index";
export default {
  name: "mobileLogin",
  data() {
    return {
      loginForm: {
        userName: "",
        password: "",
      },
    };
  },
  mounted() {
    // 如果链接中带着用户名密码， 则 填入密码后登录
    console.log(this.$route.query)
    let query = this.$route.query
    if (query.mobile) {
      this.loginForm.userName = query.mobile
      // if (query.password) {
        // this.loginForm.password = query.password
        this.loginForm.strMd = "md5"
        this.submitLoginInfo('loginFormRef')
      // }
    }

    
  },
  methods: {
      // 获得菜单codeArr
    getMenuCode(MenuTree) {
      MenuTree.map((e) => {
        this.codeArr.push(e.code);
        if (e.childList.length > 0) {
          this.getMenuCode(e.childList);
        }
      });
    },
    // 提交登录信息
    submitLoginInfo(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 为空时提示
          if (
            this.loginForm.userName === ""
            // ||
            // this.loginForm.password === ""
          ) {
            this.$message({
              type: "error",
              message: "请输入用户名及密码",
            });
            return;
          }

          const params = {
            ...this.loginForm,
            username: this.loginForm.userName,
            password: this.loginForm.password,
          };
          new Promise((resolve) => {
            userLogin(params)
              .then((res) => {
                // 获得被允许的菜单Code
                this.codeArr = [];
                this.getMenuCode(res.data.nodeList);
                const userInfo = {
                  name: res.data.userName,
                  userId: res.data.userId,
                  token: res.data.token,
                  mobile: res.data.mobile,
                  email: res.data.email,
                  roleList: res.data.roleList,
                  realName: res.data.realName,
                };
                // 将用户名和 token 写入cookie 保存 / 考虑写入loaclStorage
                this.$store.commit(
                  "app/SET_USERINFO",
                  JSON.stringify(userInfo)
                );

                // 读取名下子部门
                getAllChildList().then((m) => {
                  res.data["childDepartmentList"] = m.data.filter(
                    (w) => w.id != res.data.departmentList[0].id
                  );

                const localUserInfo = {
                  authIdArr: this.codeArr,
                  ...res.data,
                };
                // 将用户名等信息写入loaclStorage
                this.$store.commit(
                  "app/SET_LOCAL_USERINFO",
                  JSON.stringify(localUserInfo)
                );

                this.$message({
                  type: "success",
                  message: "登录成功",
                });

                // 跳转到 登录时重定向的页面 ，  如无 则去首页
                this.$router.push({
                  path: this.$route.query.redirect
                    ? this.$route.query.redirect
                    : "/mobile",
                });
                })
              })
              .catch((err) => {
                // this.loginForm={}
              });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mobile-box {
  width: 100%;
  height: 100%;
  .top {
    height: 50px;
    width: 100%;
    background-color: #f88123;
    line-height: 50px;
    color: #fff;
    text-align: center;
    font-size: 16px;
  }
  .form {
    min-height: 100px;
    padding: 20px;

    .form-bd {
      ::v-deep  .el-button--primary {
        width: 100%;
      }
    }
  }

  .undeline {
    text-align: center;
    padding: 20px 0;
    border-top: 1px solid #dddcdc;
    line-height: 20px;
    //   font-size: 12px;
    color: #666;
    a {
      color: #f88123;
    }
  }
}
</style>
